<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
</head>

<body>
    <table>
        <tr>
            <td><label for="">标题：</label></td>
            <td><input type="text" placeholder="请输入标题" name="productName"></td>
        </tr>
        <tr>
            <td><label for="">内容：</label></td>
            <td><input type="text" placeholder="请输入内容" name="neirong"></td>
        </tr>
        <tr>
            <td><label for="">摘要：</label></td>
            <td><input type="text" placeholder="请输入分类量" name="zyao"></td>
        </tr>
        <tr>
            <td><label for="">分类：</label></td>
            <td><input type="text" placeholder="请输入分类量" name="fenlei"></td>
        </tr>
        <tr>
            <td><label for="">作者：</label></td>
            <td><input type="text" placeholder="请输入作者" name="zze"></td>
        </tr>
        <tr>
            <td><input type="button" value="保存" onclick="save()"></td>
            <td><input type="button" value="取消并返回" onclick="cancel()"></td>
        </tr>
    </table>
    <script src="./js/jquery-3.6.0.min.js"></script>
    <script src="./config/baseUrl.js"></script>
    <script src="./js/api.js"></script>
    <script>
        $(function () {
           
            let id = getUrlParam('id');
            id = id || 0;
            console.log(id);
            getProductById(id).then(res => {
                console.log(res);
                let obj = res.data;
                $('[name=productName]').val(obj.productName);
                $('[name=neirong]').val(obj.neirong);
                $('[name=zyao]').val(obj.zyao);
                $('[name=fenlei]').val(obj.fenlei);
                $('[name=zze]').val(obj.zze);
            })
        })
        //获取url中的参数
        function getUrlParam(name) {

            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)"); 
            var r = window.location.search.substr(1).match(reg); 

            if (r != null) return unescape(r[2]); return null; 

        }
        function save() {
            let id = getUrlParam('id');
            console.log(id);
            let obj = {
                productName: $('[name=productName]').val(),
                neirong: $('[name=neirong]').val(),
                zyao: $('[name=zyao]').val(),
                fenlei: $('[name=fenlei]').val(),
                zze: $('[name=zze]').val(),
            }
            // 如果id存在，则表示为编辑，否则为新增
            if (id) {
                updateProduct(id, obj).then(res => {
                    console.log(res);
                    location.href = './index.html';
                })
            } else {
                let params = {
                    productName: obj.productName,
                    neirong: obj.neirong,
                    zyao: obj.zyao,
                    fenlei: obj.fenlei,
                    zze: obj.zze
                }
                addProduct(params).then(res => {
                    location.href = './index.html';
                })
            }


        }
        function cancel() {
            location.href = './index.html';
        }
    </script>
</body>

</html>